import { action, observable } from 'mobx';
import { WeaForm, WeaTableNew } from 'comsMobx';
import * as Api from './../apis/recDocManageApi';
import { WeaLocaleProvider } from 'ecCom';
import { message } from 'antd';

const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;
class RecDocManageStore {
  @observable searchFormCondition1 = [];// 未签收的form表单
  @observable searchFormCondition2 = [];// 已签收的form表单
  @observable searchFormCondition3 = [];// 已拒收、已退回、已作废的form表单
  @observable selectedKey = '0';
  @observable selectedRowKeys = [];// 选择行
  @observable searchForm = new WeaForm(); // 渲染的form
  @observable topTab = []; // toptab标签页数据
  @observable tableStore = new TableStore();
  @observable groupid = 'unreceipted';// tab页签的id
  @observable showSearchAd = false;// 高级搜索的下拉
  @observable isReject = true;// 判断是否是拒收
  @observable showReasonDialog = false;
  @observable fileId = '';// 公文的id
  @observable rejectForm = new WeaForm()// 拒收
  @observable returnForm = new WeaForm()// 退回
  @observable reciveStatus = 'unreceipted';// 查看详情时公文处于什么状态 unreceipted 未签收 receipted 已签收
  @observable showDocInfo = false;// 查看公文是否显示
  @observable documentId = '0';// baseinfoid  查看公文详情
  @observable docimagefileid = '0';// 下载正文id
  @observable attachimagefileids = '0';// 下载附件id
  // 卸载组件状态
  @action
  setSelectedRowKeys = () => {
    this.selectedRowKeys = [];
  }
  // 切换tab页
  @action.bound changeTab(key) {
    this.selectedRowKeys = [];
    this.selectedKey = key;
    switch (key) {
      // 未签收
      case '0': {
        this.groupid = 'unreceipted';
        this.reciveStatus = 'unreceipted';
        this.searchForm.setCondition(this.searchFormCondition1);
        break;
      }
      // 已签收
      case '1': {
        this.groupid = 'receipted';
        this.reciveStatus = 'receipted';
        this.searchForm.setCondition(this.searchFormCondition2);
        break;
      }
      // 已拒收
      case '2': {
        this.groupid = 'rejected';
        this.reciveStatus = '';
        this.searchForm.setCondition(this.searchFormCondition3);
        break;
      }
      // 已退回
      case '3': {
        this.groupid = 'returned';
        this.reciveStatus = '';
        this.searchForm.setCondition(this.searchFormCondition3);
        break;
      }
      // 作废
      case '4': {
        this.groupid = 'rescinded';
        this.reciveStatus = '';
        this.searchForm.setCondition(this.searchFormCondition3);
        break;
      }
    }
    this.getTableList();
  }
  // 获取所有searForm的表单数据
  @action.bound getSearchForm() {
    Api.getTabFormData().then((data) => {
      this.searchFormCondition1 = data.items1;
      this.searchFormCondition2 = data.items2;
      this.searchFormCondition3 = data.items3;
      this.topTab = data.tabs;
      this.searchForm.setCondition(this.searchFormCondition1);
    });
  }
  // 获取表格数据
  @action.bound getTableList(searchValue = '') {
    this.fileId = '';
    const form = this.searchForm.getFormParams();
    const params = {
      groupid: this.groupid, // 页签id
      documentTitle: form.documentTitle || searchValue, // 公文的标题
      issuedNumber: form.issuedNumber || '', // 发文字号
      optionsDate: form.optionsDate || '', // 日期的类型
      startDate: form.startDate || '', // 开始时间
      endDate: form.endDate || '', // 结束时间
    };
    Api.getTableList(params).then((data) => {
      if (data.sessionkey) {
        this.tableStore.getDatas(data.sessionkey, 1, 10);
        this.showSearchAd = false;
      }
    });
  }
  // 高级搜索下拉的显隐
  @action.bound setShowSearchAd() {
    this.showSearchAd = !this.showSearchAd;
  }
  // 选择行
  @action.bound onRowSelect(selectedRowKeys, selectedRows) {
    let reidArr = [];
    this.selectedRowKeys = selectedRowKeys;
    for (let row of selectedRows) {
      reidArr.push(row.id);
    }
    // this.fileId = reidArr.join(",");
  }
  // 签收
  @action.bound doReceive() {
    const params = {
      ids: this.fileId ? this.fileId : this.selectedRowKeys.join(','), // 文档id
      status: '1', // 操作类型 1：签收   2：拒收   3：退回
    };
    Api.handle(params).then((data) => {
      if (data.api_status) {
        message.success(getLabel('388093', '签收成功！'));
        if (this.showDocInfo == true) {
          this.reciveStatus = 'receipted';
          this.changeTab('1');
        } else {
          this.getTableList();
        }
        this.selectedRowKeys = [];
      } else {
        message.error(getLabel('388094', '签收失败！'));
      }
    });
  }
  // 拒收
  @action.bound doReject() {
    const formData = this.rejectForm.getFormParams();
    const params = {
      ids: this.fileId ? this.fileId : this.selectedRowKeys.join(','), // 文档id
      status: '2', // 操作类型 1：签收   2：拒收   3：退回
      detail: formData.detail,
    };
    Api.handle(params).then((data) => {
      this.rejectForm.resetForm();
      if (data.api_status) {
        message.success(getLabel('514407', '拒收成功！'));
        if (this.showDocInfo == true) {
          this.changeTab('2');
          this.reciveStatus = 'receipted';
        } else {
          this.getTableList();
        }
        this.showReasonDialog = false;
        this.selectedRowKeys = [];
      } else {
        message.success(getLabel('388124', '拒收失败！'));
      }
    });
  }
  // 退回
  @action.bound doReturn() {
    const formData = this.returnForm.getFormParams();
    const params = {
      ids: this.fileId ? this.fileId : this.selectedRowKeys.join(','), // 文档id
      status: '3', // 操作类型 1：签收   2：拒收   3：退回
      detail: formData.detail,
    };
    Api.handle(params).then((data) => {
      this.returnForm.resetForm();
      if (data.api_status) {
        message.success(getLabel('388125', '退回成功！'));
        if (this.showDocInfo == true) {
          this.changeTab('3');
          this.reciveStatus = 'receipted';
        } else {
          this.getTableList();
        }
        this.selectedRowKeys = [];
        this.showReasonDialog = false;
      } else {
        message.success(getLabel('388126', '退回失败！'));
      }
    });
  }
  // 观点退回、拒收的弹窗
  @action.bound hideReasonDialog() {
    this.showReasonDialog = false;
  }
  // 拒收form表单
  @action.bound getReason() {
    Api.getReason().then((data) => {
      this.rejectForm = new WeaForm();
      this.returnForm = new WeaForm();

      this.rejectForm.setCondition(data.conditions1);
      this.returnForm.setCondition(data.conditions2);
    });
  }
  // 右侧操作操作菜单
  @action.bound onOperatesClick(record, index, operate, flag) {
    if (!this.showDocInfo) {
      this.fileId = record.id;
    }
    switch (flag) {
      case '0': {
        this.doReceive();
        break;
      }
      case '1': {
        this.isReject = true;
        this.showReasonDialog = true;
        break;
      }
      case '2': {
        this.isReject = false;
        this.showReasonDialog = true;
        break;
      }
      // 下载正文
      case '3': {
        const docimagefileid = record.docimagefileidspan || this.docimagefileid;
        window.location.href = `${location.protocol}//${location.host}/weaver/weaver.file.FileDownload?fileid=${docimagefileid}`;
        break;
      }
      // 下载附件
      case '4': {
        const attachimagefileids = record.attachimagefileidsspan || this.attachimagefileids;
        if (attachimagefileids.includes(',')) {
          window.location.href = `${location.protocol}//${location.host}/weaver/weaver.file.FileDownload?downloadBatch=1&&fileid=${attachimagefileids}`;
        } else {
          window.location.href = `${location.protocol}//${location.host}/weaver/weaver.file.FileDownload?fileid=${attachimagefileids}`;
        }
        break;
      }
    }
  }
  // 点击文档标题
  @action.bound checkDetails(record) {
    this.documentId = record.baseinfoid;
    this.fileId = record.id;
    this.showDocInfo = true;
    this.attachimagefileids = record.attachimagefileidsspan;// 附件id
    this.docimagefileid = record.docimagefileidspan;// 正文id
  }
  @action.bound showDocInfoDialog() {
    this.showDocInfo = false;
    this.documentId = 0;
    this.fileId = '';
    this.attachimagefileids = '';// 附件id
    this.docimagefileid = '';// 正文id
  }
}
const recDocManageStore = new RecDocManageStore();
export default recDocManageStore;

